
import {lsStorage} from '@/common/cache'

let userInfo = lsStorage.get('userInfo')
let token = lsStorage.get('token')

const state = {
  token: token,
  userInfo: userInfo,
  wxConfig:null,
  navBarInfo:{
    title:'',
    desc:''
  },
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    lsStorage.set('token', token, 86400 * 7)
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo
    lsStorage.set('userInfo', userInfo, 86400 * 7)
  },
  SET_WX_CONFIG: (state, wxConfig) => {
    state.wxConfig = wxConfig
  },
  SET_NAV_BAR: (state, info) => {
    state.navBarInfo = info
  },
}

const actions = {
  updateWxConfig ({commit, state, dispatch},wxConfig) {
    commit('SET_WX_CONFIG', wxConfig)
  },
  updateNavBarInfo ({commit, state, dispatch},navBarInfo) {
    commit('SET_NAV_BAR', navBarInfo)
  },
  logout ({commit, state, dispatch}) {
    return new Promise((resolve, reject) => {
      commit('SET_USER_INFO', null)
      commit('SET_TOKEN', null)
      resolve()
    })
  },
  updateToken({commit, state, dispatch}, token) {
    return new Promise((resolve, reject) => {

      commit('SET_TOKEN', token)
      resolve()
    })
  },
  updateUserInfo ({commit, state, dispatch}, userInfo) {
    return new Promise((resolve, reject) => {
      const oldUserInfo = state.userInfo
      const newUserInfo = {
        ...oldUserInfo,
        ...userInfo
      }
      commit('SET_USER_INFO', newUserInfo)
      resolve()
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
